package com.codermn.AnnotationDemo;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author jxau_scf
 * @date 2021/8/2 - 14:38
 */
@MyAnnotation("type")
public class AnnotationtTest {
    @MyAnnotation("field")
    private String title;

    private String subtitle;

    @MyAnnotation("constructor")
    public AnnotationtTest(){}

    public AnnotationtTest(String title){
        this.title = title;
    }

    public static void main(@MyAnnotation("parameter") String[] args) {
        testAnnotation();
    }

    @Override
    @MyAnnotation("method")
    public String toString(){
        return super.toString();
    }
    private static void testAnnotation() {
        String str = "hello myannotation";
        //读取类的注解
        final Annotation[] annotations = AnnotationtTest.class.getAnnotations();
        if (AnnotationtTest.class.isAnnotationPresent(MyAnnotation.class)){
            MyAnnotation anno = AnnotationtTest.class.getAnnotation(MyAnnotation.class);
            System.out.println("类:"+AnnotationtTest.class.getName()+" 读取注解描述:"+anno.value());
        }

        //读取成员方法的注解信息
        Method[] declaredMethods = AnnotationtTest.class.getDeclaredMethods();
        for (Method method : declaredMethods){
            if (method.isAnnotationPresent(MyAnnotation.class)){
                MyAnnotation anno = method.getAnnotation(MyAnnotation.class);
                System.out.println("方法:"+method.getName()+"读取注解描述:"+anno.value());
            }
        }

        //读取成员变量的信息
        Field[] declaredFields = AnnotationtTest.class.getDeclaredFields();
        for (Field field : declaredFields){
            if (field.isAnnotationPresent(MyAnnotation.class)){
                MyAnnotation anno = field.getAnnotation(MyAnnotation.class);
                System.out.println("成员变量："+field.getName()+"读取注解描述："+anno.value());
            }
        }
    }
}
